﻿package com.eve.microworld.common.app;

import android.app.Application;
import android.content.Context;

import com.eve.microworld.common.db.SQLiteAccess;
import com.eve.microworld.R;
import com.eve.microworld.common.app.Constants;
import com.eve.microworld.common.tools.ComTimeTools;
import com.eve.microworld.common.tools.ConfigurationTools;
import com.eve.microworld.common.tools.ComDeviceTools;
import com.eve.microworld.common.tools.LogTools;

/**
 * 全集Application信息
 * 
 * @author Norsie
 * @create 12-11-28
 * 
 */
public class BaseApplication extends Application
{

    /** 应用程序名称 **/
    public static String AppName = null;

    /** 数据库访问操作 **/
    public static SQLiteAccess mDBAccess = null;

    /** ApplicationContext实例 **/
    public static Context mAppContext = null;

    /** 全局共享对象 **/
    public static Object mShareObj = null;

    /** 网络连接方式 **/
    public static String mNetStyle = null;
    public static int mNetStyleCode = -1;

    /** 屏幕尺寸 **/
    public static int DisplayWidth = 480;

    public static int DisplayHeight = 800;

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();

        AppName = getResources().getString(R.string.app_name);

        mNetStyle = getReceiver();

        mAppContext = getApplicationContext();

        setFirstLoginTime();

    }

    /**
     * 设置第一次登陆的时间
     */
    private void setFirstLoginTime()
    {
        // TODO Auto-generated method stub
        String time=ComTimeTools.getTime("yyyy-MM-dd HH:mm:ss");
        if (ConfigurationTools.getInstance(mAppContext).getString(
                Constants.FIRST_LOGIN_TIME, "").equals(""))
        {
            ConfigurationTools.getInstance(mAppContext).putString(
                    Constants.FIRST_LOGIN_TIME, time);
            LogTools.info("Set First Login Time:" + time);
        }
        else
            LogTools.info("First Login Time:"
                    + ConfigurationTools.getInstance(mAppContext).getString(
                            Constants.FIRST_LOGIN_TIME));
    }

    /**
     * 获取网络请求管理器.
     * 
     * @return SQLiteAccess
     */
    public static SQLiteAccess getFpiSQLiteAccess()
    {
        if (mDBAccess == null)
        {
            mDBAccess = SQLiteAccess.getInstance(mAppContext);
        }
        return mDBAccess;
    }

    /**
     * 网络处理
     */
    private String getReceiver()
    {
        String mNetStyle = null;
        mNetStyleCode = ComDeviceTools.checkNetWorkType(getApplicationContext());
        switch (mNetStyleCode)
        {
            case Constants.NETWORK_TYPE_NONE:
                mNetStyle = Constants.NETWORK_TYPE_NONE_MSG;
                break;

            case Constants.NETWORK_TYPE_WIFI:
                mNetStyle = Constants.NETWORK_TYPE_WIFI_MSG;
                break;

            case Constants.NETWORK_TYPE_LOW:
                mNetStyle = Constants.NETWORK_TYPE_LOW_MSG;
                break;

            case Constants.NETWORK_TYPE_HIGH:
                mNetStyle = Constants.NETWORK_TYPE_HIGH_MSG;
                break;
            default:
                break;
        }
        return mNetStyle;
    }

}
